﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Tracking.Data.Model.Interfaces;
using System.Dynamic;
using System.Linq;

namespace Tracking.Data.Model.Implementation
{
    public class TrackingRepository : ITrackingRepository
    {
        public Shipment GetInfo(string trackingNumber)
        {
            var context = new TrackingModelContainer();
            var item = context.Shipments.FirstOrDefault(p => p.TrackingNumber == trackingNumber);
            return item;
        }

        //public IQueryable<Shipment> GetList(int page, int rows, int x)
        //{
        //    var newList = base.Get<AuditLog>(list, out total, page, rows, _search, searchField, searchOper, searchString, expr, sidx, sord).ToList();


        //}

        public IQueryable<Shipment> Get(out int total, int index = 0, int size = 50, Expression<Func<Shipment, bool>> filter = null, string orderBy = null)
        {
            var context = new TrackingModelContainer();
            var itemList = context.Shipments.AsQueryable();
            int skipCount = (index - 1) * size;

            var resetSet = filter != null ? itemList.Where(filter).AsQueryable() : itemList.AsQueryable();

            resetSet = orderBy != null ? resetSet.OrderBy(orderBy).AsQueryable() : resetSet.AsQueryable();

            total = resetSet.Count();

            resetSet = skipCount == 0 ? resetSet.Take(size) : resetSet.Skip(skipCount).Take(size);

            return resetSet.AsQueryable();
        }



        public void Create(Shipment shipment)
        {
            var context = new TrackingModelContainer();
            shipment.State = (byte)ShipmentStates.Started;
            context.Shipments.Add(shipment);

            //TODO: add commit method in base class
            context.SaveChanges();
        }


        public Shipment GetById(int id)
        {
            var context = new TrackingModelContainer();
            return context.Shipments.FirstOrDefault(p => p.Id == id);
        }


        public void Update(Shipment shipment)
        {
            var context = new TrackingModelContainer();
            var existing = context.Shipments.First(p => p.Id == shipment.Id);
            existing.LastUpdatedTimestamp = DateTime.Now;
            existing.State = shipment.State;
            context.SaveChanges();
        }


        public void Delete(int id)
        {
            var context = new TrackingModelContainer();
            var existing = context.Shipments.FirstOrDefault(p => p.Id == id);
            if (existing != null)
            {
                context.Shipments.Remove(existing);
                context.SaveChanges();
            }
        }


        public Shipment GetByTrackingNumber(string trackingNr)
        {
            var context = new TrackingModelContainer();
            var shipment = context.Shipments.FirstOrDefault(p => p.TrackingNumber == trackingNr);
            return shipment;
        }
    }
}
